using Godot;
using System;
using System.Collections.Generic;

public partial class TimerAddRobot : Node2D
{
    [Signal] public delegate void OnAddRobotEndEventHandler();
    Timer timer;
    static RobotManager RobotManager => GameManager.Instance.LevelManager.RobotManager;

    List<LevelRobotModel> addingRobots;
    int index = 0;
    bool isPlayer;
    bool isAlly;

    static Cursor Cursor => GameManager.Instance.LevelManager.Cursor;

    public override void _Ready()
    {
        timer = GetNode<Timer>("Timer");
        timer.Timeout += OnTimerTimeoutHandler;
    }

    private void OnTimerTimeoutHandler()
    {
        if (index >= addingRobots.Count)
        {
            timer.Stop();
            EmitSignal(SignalName.OnAddRobotEnd);
            return;
        }
        else
        {
            if (isAlly)
            {
                var r = RobotManager.CreateAllyRobot(addingRobots[index]);
                Cursor.LookAt(r);
            }
            else if (isPlayer)
            {
                var r = RobotManager.CreatePlayerRobot(addingRobots[index]);
                Cursor.LookAt(r);
            }
            else
            {
                var r = RobotManager.CreateEnemyRobot(addingRobots[index]);
                Cursor.LookAt(r);
            }
            index++;
        }
    }

    public void Start(List<LevelRobotModel> list, bool player, bool ally)
    {
        addingRobots = list;
        isPlayer = player;
        isAlly = ally;
        timer.Start();
    }

}
